package com.cheetah.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @Author Administrator
 * @Date 2020/8/27
 * @Version V1.0
 * @Description:
 **/
@Component
@Slf4j
public class JsonUtil {

    private ObjectMapper objectMapper = new ObjectMapper();

    public String toJsonStr(Object o){
        try {
            return objectMapper.writeValueAsString(o);
        } catch (JsonProcessingException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public <T> T toObject(String s,Class<T> clazz){
        try {
            return objectMapper.readValue(s,clazz);
        } catch (JsonProcessingException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }
}
